<?php

namespace Modules\Idoc\UI\Component;

use Duxravel\Core\UI\Widget\TreeList;
use Illuminate\View\Component;

class TreeView extends Component
{

    private $key;
    private string $field;
    private string $event;
    private ?string $url = null;
    private ?string $sortUrl = null;
    private ?string $filter = null;
    private bool $search = true;
    private array $keyword = [];
    private array $menu = [];
    private array $fieldNames = [];
    private $labelNode = [];

    public $data = [];

    public function __construct( $data = []){
        $this->data = $data;
    }

    /**
     * @param bool  $bool
     * @param array $keyword
     * @return $this
     */
    public function search(bool $bool = true, array $keyword = []): self
    {
        $this->search = $bool;
        $this->keyword = $keyword;
        return $this;
    }

    /**
     * @param array $data
     * @return $this
     */
    public function menu(array $data = []): self
    {
        $this->menu = $data;
        return $this;
    }

    /**
     * @param string|null $url
     * @return $this
     */
    public function url(string $url = null): self
    {
        $this->url = $url;
        return $this;
    }

    /**
     * @param string|null $url
     * @return $this
     */
    public function sortUrl(string $url = null): self
    {
        $this->sortUrl = $url;
        return $this;
    }

    /**
     * @param string $filter
     * @return $this
     */
    public function filter(string $filter): self
    {
        $this->filter = $filter;
        return $this;
    }

    public function fieldNames(array $map): self
    {
        $this->fieldNames = $map;
        return $this;
    }

    /**
     * @param $node
     * @return $this
     */
    public function label($node): TreeList
    {
        $this->labelNode = $node;
        return $this;
    }


    /**
     * @return mixed
     */
    public function render()
    {
//        $urlPaths = parse_url(substr($this->url, 0, strrpos($this->url, "/")));
//        $tree = [
//            'url' => $this->url,
//            'sortUrl' => $this->sortUrl,
//            'search' => $this->search,
//            'keywords' => $this->keyword,
//            'requestEventName' => md5($this->event ?: url_class($this->url)['class']),
//            'vBind:filter' => $this->filter ?: '',
//            'refreshUrls' => [trim($urlPaths['path'], '/')],
//            'iconColor' => ['blue', 'cyan', 'green', 'orange', 'red', 'purple'],
//            'vModel:value' => "data.filter['{$this->field}']",
//        ];
//
//        if ($this->fieldNames) {
//            $tree['fieldNames'] = $this->fieldNames;
//        }
//
//        if ($this->labelNode) {
//            $tree['child'] = [
//                'nodeName' => 'span',
//                'vSlot:label' => 'item',
//                'child' => $this->labelNode
//            ];
//        }
//
//        $menu = [];
//        if ($this->menu) {
//            foreach ($this->menu as $key => $vo) {
//                $url = $vo['url'];
//                $event = $vo['event'];
//                $tmp = [
//                    'text' => $vo['name'],
//                ];
//                $tmp['key'] = $key;
//                if ($event) {
//                    $tmp['event'] = $event;
//                } else {
//                    switch ($vo['type']) {
//                        case 'dialog':
//                            $tmp['event'] = $url ? "window.router.dialog($url)" : "window.dialog.alert({content: '未定义链接数据'})";
//                            break;
//                        case 'ajax':
//                            $tmp['event'] = $url ? "window.router.ajax($url, {_method: 'POST', _title: '确认进行{$vo['name']}操作？'})" : "window.dialog.alert({content: '未定义链接数据'})";
//                            break;
//                        default:
//                            $tmp['event'] = $url ? "window.router.push($url)" : "window.dialog.alert({content: '未定义链接数据'})";
//                    }
//                }
//                $menu[] = $tmp;
//            }
//            $tree['contextMenus'] = $menu;
//        }
        return view('modules.Idoc.View.Component.TreeView');
    }

}
